package com.jinmdz.fmis.dao.admin.dao.provider;

import com.jinmdz.fmis.dao.admin.model.accountuser.AccountUserListData;
import com.jinmdz.fmis.dao.base.BaseProvider;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

/**
 * 帐号用户维护Provider
 *
 * @author LiCongLu
 * @date 2020-03-17 09:37
 */
public class AccountUserProvider extends BaseProvider {

    /**
     * 查询所有帐号用户信息
     *
     * @param data 查询条件
     * @return
     * @author LiCongLu
     * @date 2020-03-17 09:37
     */
    public String listAccountUserList(@Param("data") AccountUserListData data) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" system_user.id,job_no,full_name,user_gender_code");
                builder.append(" ,mobile_phone,other_phone,system_user.address,company_id,department_id,system_user.disable_state ");
                builder.append(" ,as_login,ic_card_code,system_user.sort,system_user.remark,system_user.version ");
                builder.append(" ,company.organization_name as company_name,department.organization_name as department_name");
                SELECT(builder.toString());
                FROM(" system_user ");
                LEFT_OUTER_JOIN(" organization_institution company on company.id = company_id ");
                LEFT_OUTER_JOIN(" organization_institution department on department.id = department_id ");

                WHERE(" system_user.deleted = 0 ");

                // 判断员工编号
                if (valid(data.getJobNo())) {
                    WHERE(" job_no like concat('%',#{data.jobNo},'%') ");
                }

                // 判断姓名
                if (valid(data.getFullName())) {
                    WHERE(" full_name like concat('%',#{data.fullName},'%') ");
                }

                // 判断公司名称
                if (valid(data.getCompanyName())) {
                    WHERE(" company.organization_name like concat('%',#{data.companyName},'%') ");
                }

                // 判断部门名称
                if (valid(data.getDepartmentName())) {
                    WHERE(" department.organization_name like concat('%',#{data.departmentName},'%') ");
                }

                // 判断有效状态
                if (validAll(data.getDisableState())) {
                    WHERE(" system_user.disable_state = #{data.disableState} ");
                }

                ORDER_BY(" system_user.job_no ");
            }
        }.toString();
    }
}
